package com.yuju.rpt.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.account.service.IAccountService;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.log.model.LogBizRecordModel;
import com.yuju.log.service.ILogBizRecordService;
import com.yuju.order.model.ReserveModel;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.order.service.IReserverService;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.rpt.dao.IRptOrderDao;
import com.yuju.rpt.service.IExportForOrderService;
import com.yuju.util.ExcelExp;
import com.yuju.util.Format;
import com.yuju.util.Utils;

/**
 * 
 * 类名：OrderRptServiceImpl  <br />
 *
 * 功能：订单报表
 *
 * @author : sunyl <br />
 * @Date : 2018年1月24日 下午1:54:33  <br />
 * @version : 1.1 <br />
 */
@Service
public class ExportForOrderServiceImpl implements IExportForOrderService {
	@Autowired
	private IAccountService accountService;
	@Autowired
	private ILogBizRecordService logBizRecordService;
	@Autowired
	private IRoomOrderService roomOrderService;
	@Autowired
	private IReserverService reserverService;
	/**
	 * 
	 * 功能: <br/>
	 * 
	 * 重写：xuehj <br/>
	 * sunyl 预离
	 * @Date ：2018年2月1日 下午4:23:44<br/>
	 * 
	 * @param model
	 * @return
	 * @throws IOException <br/>
	 * @see com.yuju.rpt.service.IExportForOrderService#expCheckoutRpt(com.yuju.order.model.RoomOrderModel)
	 */
	@Override
	public byte[] expCheckoutRpt(RoomOrderModel model) throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		
		e.setCell(0, "入住时间");
		e.setCell(1, "预离时间");
		e.setCell(2, "房号");
		e.setCell(3, "姓名");
		e.setCell(4, "客户");
		e.setCell(5, "入住标准");
		e.setCell(6, "房价");
		e.setCell(7, "总消费");
		e.setCell(8, "余额");
		e.setCell(9, "在住状态");
		List<RoomOrderModel> list = roomOrderService.list(model);
		if (Utils.isNull(list) || list.isEmpty()) {
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		//根据订单号获取总消费和总应收
		BigDecimal sumTotalForXF = new BigDecimal(0);
		BigDecimal sumTotalForBalance = new BigDecimal(0);
		for(RoomOrderModel item:list){
			Map<String, Object> detailByOrderId = accountService.getDetailByOrderId(item.getId());
			if(detailByOrderId != null){
				//总消费
				item.setTotalBill(detailByOrderId.get("ysSum"));
				//总收款
				item.setSettlementTotal(detailByOrderId.get("ssSum"));
				sumTotalForBalance = sumTotalForBalance.add(item.getBalance());
				sumTotalForXF = sumTotalForXF.add(new BigDecimal(detailByOrderId.get("ysSum").toString()));
			}
		}
		
		for(int i=0; i<list.size(); i++) {
			double totalBill = 0;
			double settlementTotal = 0;
			Object val = list.get(i).getTotalBill();
			if (val instanceof BigDecimal) {
				totalBill = ((BigDecimal) val).doubleValue();
			}
			
			val = list.get(i).getSettlementTotal();
			if (val instanceof BigDecimal) {
				settlementTotal = ((BigDecimal) val).doubleValue();
			}
			
			e.createRow(i+1);
			e.setCell(0, Format.getDateStr(list.get(i).getCheckinTime()));
			e.setCell(1, Format.getDateStr(list.get(i).getCheckoutTime()));
			e.setCell(2, list.get(i).getRoomCode());
			e.setCell(3, list.get(i).getName());
			e.setCell(4, list.get(i).getCunstomName());
			e.setCell(5, list.get(i).getCheckinStandardName());
			e.setCell(6, list.get(i).getRoomPrice()!=null?list.get(i).getRoomPrice().toString():"0");
			e.setCell(7, totalBill);
			e.setCell(8,list.get(i).getBalance().doubleValue());
			e.setCell(9, list.get(i).getCheckinStatusName());
		}
		e.createRow(list.size()+1);
		e.setCell(6,"合计");
		e.setCell(7, sumTotalForXF.toString());
		e.setCell(8,sumTotalForBalance.toString());
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}
	/**
	 * 
	 * 功能: <br/>
	 * sunyl 
	 * 预抵报表导出
	 * 重写：xuehj <br/>
	 * 
	 * @Date ：2018年2月1日 下午4:24:08<br/>
	 * 
	 * @param model
	 * @return
	 * @throws IOException <br/>
	 * @see com.yuju.rpt.service.IExportForOrderService#expCheckinRpt(com.yuju.order.model.ReserveModel)
	 */
	@Override
	public byte[] expCheckinRpt(ReserveModel model) throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		
		e.setCell(0, "预订单号");
		e.setCell(1, "姓名");
		e.setCell(2, "预抵时间");
		e.setCell(3, "预离时间");
		e.setCell(4, "房数");
		e.setCell(5, "房间");
		e.setCell(6, "客户");
		e.setCell(7, "备注");
		
		List<ReserveModel> list = reserverService.list(model);
		if (Utils.isNull(list) || list.isEmpty()) {
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		reserverService.actionListinfo(list);
		for(int i=0; i<list.size(); i++) {
			e.createRow(i+1);
			e.setCell(0, list.get(i).getReserveNo());
			e.setCell(1, list.get(i).getReserveName());
			e.setCell(2, Format.getForMatDayForFull(list.get(i).getInTime()));
			e.setCell(3, Format.getDateStr(list.get(i).getOutTime()));
			e.setCell(4, list.get(i).getRoomCount());
			e.setCell(5, list.get(i).getRoomStr());
			e.setCell(6, list.get(i).getCustomName());
			e.setCell(7, list.get(i).getRemark());
		}
		/**
		 * 附加统计数据
		 */
		Map<Object, Object> param = new HashMap<>();
		param.put("businessTime",model.getBusinessTime());
		param.put("hotelId",model.getHotelId());
		param.put("groupId",model.getGroupId());
		List<Map<String,Object>> dataList = reserverService.selectReserveCountInfo(param);
		e.createRow(list.size()+1);
		e.setCell(3, "房型统计");
		e.createRow(list.size()+2);
		e.setCell(2, "房型");
		e.setCell(4, "数量");
		if(dataList!=null && dataList.size()>0){
			for(int i=0; i< dataList.size(); i++){
				e.createRow(list.size()+i+3);
				Object roomTypeName = dataList.get(i).get("roomTypeName");
				e.setCell(2, roomTypeName!=null?roomTypeName.toString():"");
				Object count = dataList.get(i).get("count");
				e.setCell(4, count.toString());
			}
		}
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}
	/**
	 * 
	 * 功能: <br/>
	 * 
	 * sunyl
	 * 重写：xuehj <br/>
	 * @Date ：2018年2月1日 上午11:15:31<br/>
	 * 
	 * @param model
	 * @return
	 * @throws IOException <br/>
	 * @see com.yuju.rpt.service.IExportForOrderService#expOrderChangeExcel(com.yuju.log.model.LogBizRecordModel)
	 */
	@Override
	public byte[] expOrderChangeExcel(LogBizRecordModel model) throws IOException{
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		e.setCell(0, "房号");
		e.setCell(1, "时间");
		e.setCell(2, "异动类型");
		e.setCell(3, "原房号");
		e.setCell(4, "新房号");
		e.setCell(5, "原价格方案");
		e.setCell(6, "新价格方案");
		e.setCell(7, "原价格");
		e.setCell(8, "新价格");
		
		e.setCell(9, "员工");
		e.setCell(10, "备注");
		
		//model.setCheckoutTime(model.getdu);
		List<LogBizRecordModel> list = logBizRecordService.list(model);
		if (Utils.isNull(list) || list.isEmpty()) {
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		roomOrderService.formatLogRecordByDetail(list);
				
		for(int i=0; i<list.size(); i++) {
			e.createRow(i+1);
			e.setCell(0, list.get(i).getRoomCode());
			e.setCell(1, Format.getForMatDayForFull(list.get(i).getGmtCreate()));
			e.setCell(2, list.get(i).getBizTypeName());
			e.setCell(3, list.get(i).getOldRoomCode());
			e.setCell(4, list.get(i).getNewRoomCode());
			e.setCell(5, list.get(i).getOldPricePlan());
			e.setCell(6, list.get(i).getNewPricePlan());
			e.setCell(7, list.get(i).getOldRoomPrice());
			e.setCell(8, list.get(i).getNewRoomPrice());
			e.setCell(9, list.get(i).getCreateUserName());
			e.setCell(10, list.get(i).getRemark());
		}
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}
}
